﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using ClassLibrary.Utilities;

public partial class Admin_ucControl_ucThemAdmin : System.Web.UI.UserControl
{
    TblUserAccountDAO userDAO = new TblUserAccountDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.SESSION_ADMIN_USER] == "" || Session[Global.SESSION_ADMIN_USER] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            loadAdmin();
        }
    }

    private void loadAdmin()
    {
        grdAdmin.DataSource = userDAO.GetAll();
        grdAdmin.DataBind();
    }
    private bool UserAccount_CheckExists()
    {
        bool bExists = false;
        try
        {
            bExists = userDAO.CheckExits(txtEmail.Text, 1);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Admin/Error.aspx", true);
        }
        return bExists;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = true;
        lblMessage.Text = String.Empty;

        if (UserAccount_CheckExists())
        {
            lblMessage.Text = "Email đã tồn tại.";
            return;
        }
        try
        {
            TblUserAccount user = new TblUserAccount();
            user.Address = txtAdress.Text;
            user.Email = txtEmail.Text;
            user.IsAdmin = true;
            user.MobilePhone = txtphone.Text;
            user.Name = txtFName.Text;
            user.Pass =  Encryption.EncryptToString(txtPassword.Text);

            user.ID = 0;
            userDAO.Insert_Update(user, 1);

        }
        catch(Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            Response.Redirect("~/Admin/DanhSachAmin.aspx");
        }
    }
    protected void grdAdmin_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
    }
}
